package logic

import (
	"context"
	"errors"
	"shop/cache"
	"shop/model/mysql"

	"shop/internal/svc"
	"shop/shop"

	"github.com/zeromicro/go-zero/core/logx"
)

type HouseUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewHouseUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *HouseUpdateLogic {
	return &HouseUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *HouseUpdateLogic) HouseUpdate(in *shop.HouseUpdateRequest) (*shop.HouseUpdateResponse, error) {
	// todo: add your logic here and delete this line
	var err error
	result, _ := cache.Rdb.Get(l.ctx, "shopAdd").Result()
	if result != "" {
		logx.Error("商品已经超过三十天为出售")
		return nil, errors.New("商品已经超过三十天为出售")
	}
	var h mysql.House
	h.UserId = int(in.UserId)
	h.Status = int(in.Status)
	mysql.DB.Where("id = ?", in.Id).Find(&h)
	err = mysql.DB.Where("id = ?", in.Id).Updates(&h).Error
	if err != nil {
		logx.Error("修改失败")
		return nil, errors.New("修改失败")
	}
	return &shop.HouseUpdateResponse{
		Id: int64(h.ID),
	}, nil
}
